﻿using Microsoft.AspNetCore.Http;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using UtilsCache;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace GatewayServerService.Data
{
    public class DBIotDeviceCache
    {


        /// <summary>
        /// 缓存增加 1分钟
        /// </summary>
        /// <param name="dguid"></param>
        /// <param name="min"></param>
        /// <returns></returns>
        public iot_device getClientDeviceByCache(string dguid)
        {
            // UtilRedisNewLife UtilRedisNewLife = UtilRedisNewLife;
            iot_device device = UtilMemoryCacheHelper.get<iot_device>("iot_device_1_" + dguid);//数据太多，报错
            if (device == null)
            {
                EFYihongIotContext ef = new EFYihongIotContext();
                device = ef.iot_device.Where(c => c.d_guid == dguid && c.d_status == 0).FirstOrDefault();
                if (device == null)
                {
                    return null;
                }
                UtilMemoryCacheHelper.set("iot_device_1_" + dguid, device, 1);
            }
            return device;// ef.iot_device.Where(c => c.d_guid == dguid && c.d_status == 0).FirstOrDefault(); ;
        }

        /// <summary>
        /// 缓存增加 60分钟
        /// </summary>
        /// <param name="dguid"></param>
        /// <returns></returns>
        public iot_device getClientDeviceByCache_60Min(string dguid)
        {
            iot_device device = UtilMemoryCacheHelper.get<iot_device>("iot_device_60_" + dguid);//数据太多，报错
            if (device == null)
            {
                EFYihongIotContext ef = new EFYihongIotContext();
                device = ef.iot_device.Where(c => c.d_guid == dguid && c.d_status == 0).FirstOrDefault();
                if (device == null)
                {
                    return null;
                }
                UtilMemoryCacheHelper.set("iot_device_60_" + dguid, device, 60);
            }
            return device;// ef.iot_device.Where(c => c.d_guid == dguid && c.d_status == 0).FirstOrDefault(); ;
        }
    }
}
